﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using HotelManagement._04.OBJECT;
using HotelManagement._02.BUS;
using HotelManagement;

namespace HotelManagement._01.FORM
{
    public partial class F41_ServiceManagement : Form
    {
        Service m_Service;
        ServiceBUS m_ServiceBUS;
        public F41_ServiceManagement()
        {
            InitializeComponent();
            Init();
            LoadDataToGrid();
            if (AssetsGS.instance.RoleUser == "User")
            {
                m_btn_Delete.Enabled = false;
                m_btn_Add.Enabled = false;
                m_btn_Update.Enabled = false;
            }
        }
        private void Init()
        {
            m_Service = new Service();
            m_ServiceBUS = new ServiceBUS();
        }
        private void LoadDataToGrid()
        {
            m_dgv_Service.DataSource = m_ServiceBUS.ViewService();
        }
        private Service GetDataFromForm()
        {
            Service v_Service = new Service();
            v_Service.ServiceDescription = m_txt_ServiceDescription.Text;
            if (CValidate.IsNumber(m_txt_ServiceCost.Text)) v_Service.ServiceCost = float.Parse(m_txt_ServiceCost.Text);
            v_Service.Note = m_txt_Note.Text;
            if (CValidate.IsNumber(m_txt_ServiceID.Text)) v_Service.ServiceID = int.Parse(m_txt_ServiceID.Text);
            return v_Service;
        }
        private void SetDataToForm(Service ip_Service)
        {
            m_txt_ServiceCost.Text = ip_Service.ServiceCost.ToString();
            m_txt_ServiceDescription.Text = ip_Service.ServiceDescription;
            m_txt_Note.Text = ip_Service.Note;
            m_txt_ServiceID.Text = ip_Service.ServiceID.ToString();
        }
        private void ResetText()
        {
            m_txt_ServiceDescription.Text="";
            m_txt_Note.Text="";
            m_txt_ServiceCost.Text="";
            m_txt_ServiceID.Text = "";
        }
        private bool ValidateForm()
        {
            if (CValidate.IsNullString(m_txt_ServiceDescription.Text)) { MessageBox.Show("Bạn chưa chọn loại dịch vụ nào !"); return false; }
            if (!CValidate.IsNumber(m_txt_ServiceCost.Text)) { MessageBox.Show("Bạn chưa nhập giá dịch vụ hoặc giá dịch vụ chưa đúng định dạng !"); return false; }
            return true;

        }
        private void Cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void Add_Click(object sender, EventArgs e)
        {
            if (m_txt_ServiceID.Text.Trim() != "") return;
            if (!ValidateForm()) return;
            try 
	        {   
                m_Service = GetDataFromForm();
		        if (m_ServiceBUS.AddService(m_Service)) MessageBox.Show("Thêm dịch vụ thành công");
	        }
	        catch (Exception v_e)
	        {
		        throw v_e;
	        }
            ResetText();
            LoadDataToGrid();
        }

        private void Update_Click(object sender, EventArgs e)
        {
            if (m_txt_ServiceID.Text.Trim() == "") return;
            if (!ValidateForm()) return;
            try
            {
                m_Service = GetDataFromForm();
                if (m_ServiceBUS.UpdateService(m_Service)) MessageBox.Show("Sửa dịch vụ thành công");
            }
            catch (Exception v_e)
            {
                throw v_e;
            }
            ResetText();
            LoadDataToGrid();
        }

        private void m_dgv_Service_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                m_Service.ServiceID =int.Parse(m_dgv_Service.Rows[m_dgv_Service.CurrentCell.RowIndex].Cells["ServiceID"].Value.ToString());
                m_Service.ServiceDescription = m_dgv_Service.Rows[m_dgv_Service.CurrentCell.RowIndex].Cells["ServiceDescription"].Value.ToString();
                m_Service.ServiceCost = float.Parse(m_dgv_Service.Rows[m_dgv_Service.CurrentCell.RowIndex].Cells["ServiceCost"].Value.ToString());
                m_Service.Note = m_dgv_Service.Rows[m_dgv_Service.CurrentCell.RowIndex].Cells["Note"].Value.ToString();
                SetDataToForm(m_Service);
            }
            catch (Exception)
            {

                MessageBox.Show("Bạn chưa chọn loại dịch vụ nào !");
            }
        }

        private void Delete_Click(object sender, EventArgs e)
        {
            if (m_txt_ServiceID.Text.Trim() == "") return;
            if (MessageBox.Show("Bạn có chắc là bạn muốn xóa thông tin này ?", "Xác nhận", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
            {
                if (m_ServiceBUS.DeleteService(m_Service)) MessageBox.Show("Đã xóa thành công !"); else MessageBox.Show("Lỗi không thể xóa !");
                LoadDataToGrid();
            }
            ResetText();
        }

        private void Reset(object sender, EventArgs e)
        {
            ResetText();
        }

        private void search_Click(object sender, EventArgs e)
        {
            m_dgv_Service.DataSource = m_ServiceBUS.SearchService(m_txt_Keyword.Text);
            m_txt_Keyword.Text = "";
        }

        private void m_dgv_Service_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }        

    }
}
